package com.xzr.yespaper.controller;

import com.xzr.yespaper.bean.Files;
import com.xzr.yespaper.bean.User;
import com.xzr.yespaper.service.FilesService;
import com.xzr.yespaper.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RegisterController{

    @Autowired
    UserService userService;

    @Autowired
    FilesService filesService;

    /**
     * To register page
     * @return register.html
     */
    @GetMapping("/register")
    public String me(){
        return "register";
    }

    /**
     * To protocol page
     * @return protocol.html
     */
    @GetMapping("/protocol")
    public String protocol(){
        return "protocol";
    }

    /**
     * 注册功能 Register
     * @param username 用户名
     * @param password 密码
     * @param type 类型
     * @param email 邮箱
     * @param university 学校
     * @return JSON {code: true/false, msg: 提示信息}
     * @throws JSONException
     */
    @PostMapping("/create")
    @ResponseBody
    public String doCreate(@RequestParam("username") String username,
                         @RequestParam("password") String password,
                         @RequestParam("position") String type,
                         @RequestParam("email") String email,
                         @RequestParam("university") String university) throws JSONException {
        if(userService.createUserAccount(username, password, type, email, university, "all")){
            JSONObject ret = new JSONObject();
            ret.put("code",true);
            ret.put("msg","Registration succeed.");
            //System.out.println(ret);
            return ret.toString();
        }else{
            JSONObject ret = new JSONObject();
            ret.put("code",false);
            ret.put("msg","Your registered user name already exists, please try another one.");
            //System.out.println(ret);
            return ret.toString();
        }
    }

    /**
     * 修改信息 Edit user info
     * @param user com.xzr.yespaper.bean.User
     * @return JSON {code: true/false, msg: 提示信息(prompt message)}
     * @throws JSONException
     */
    @PostMapping("/updateinfo")
    @ResponseBody
    public String doUpdate(User user) throws JSONException {
        if(userService.updateUserInfo(user)){
            JSONObject ret = new JSONObject();
            ret.put("code",true);
            ret.put("msg","Update succeed.");
            //System.out.println(ret);
            return ret.toString();
        }else{
            JSONObject ret = new JSONObject();
            ret.put("code",false);
            ret.put("msg","Update failed, please try again.");
            //System.out.println(ret);
            return ret.toString();
        }
    }
    /**
     * 修改密码 Edit password
     * @param user com.xzr.yespaper.bean.User
     * @return JSON {code: true/false, msg: 提示信息(prompt message)}
     * @throws JSONException
     */
    @PostMapping("/updatepass")
    @ResponseBody
    public String changePass(User user) throws JSONException {
        if(userService.updatePassword(user)){
            JSONObject ret = new JSONObject();
            ret.put("code",true);
            ret.put("msg","Successfully Updated.");
            //System.out.println(ret);
            return ret.toString();
        }else{
            JSONObject ret = new JSONObject();
            ret.put("code",false);
            ret.put("msg","Update failed, please try again.");
            //System.out.println(ret);
            return ret.toString();
        }
    }
}